package com.party.controller.sys.exam;

import com.party.common.Page;
import com.party.model.Exam;
import com.party.model.ExamAnswer;
import com.party.model.ExamTest;
import com.party.model.SysUser;
import com.party.service.exam.ExamAnswerService;
import com.party.service.exam.ExamService;
import com.party.service.exam.ExamTestService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/examTest")
public class SysExamTestController {

    @Resource
    ExamTestService examTestService;

    @Resource
    ExamService examService;

    @Resource
    ExamAnswerService examAnswerService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/examtest/index");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String name = req.getParameter("name");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        ExamTest examTest = new ExamTest();
        examTest.setTitle(title);
        examTest.setName(name);
        Page p = this.examTestService.getExamTestTestList(examTest, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount());
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/examtest/add");
        List<Exam> listExam=examService.getAllExam();
        modelAndView.addObject("listExam",listExam);
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
            @ModelAttribute("examTest") ExamTest examTest,
            @RequestParam("data") String data,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {

        String msg="";
        Map<String, Object> map = new HashMap<String, Object>();

        if("".equals(data)){
            msg="添加失败";
            map.put("msg",msg);
            return map;
        }
        String [] dataArray=data.split("Θ");
        List<ExamAnswer> listExamAnswer=new ArrayList<ExamAnswer>();
        for(String result:dataArray){
            String[] examAnswerArray=result.split("Γ");
            ExamAnswer examAnswer=new ExamAnswer();
            examAnswer.setContent(examAnswerArray[0]);
            if("false".equals(examAnswerArray[1])){
                examAnswer.setIsright("0");
            }else{
                examAnswer.setIsright("1");
            }
            listExamAnswer.add(examAnswer);
        }

        int num=examTestService.addExamTestAndExamAnswer(examTest,listExamAnswer);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }
        map.put("msg",msg);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String msg="";
        ExamTest examTest=null;
        List<ExamAnswer> listExamAnswer=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            examTest=examTestService.getExamTestByPrimaryKey(intId);
            listExamAnswer=examAnswerService.getListExamAnswerByExamTestPrimaryKey(intId);

        }

        if(examTest==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }

        modelAndView.addObject("examTest",examTest);
        modelAndView.addObject("listExamAnswer",listExamAnswer);
        modelAndView.setViewName("/sys/examtest/edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @ModelAttribute("examTest") ExamTest examTest,
            @RequestParam("data") String data,
            @RequestParam("deleteExamTestIds") String deleteExamTestIds,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        String msg="";
        Map<String, Object> map = new HashMap<String, Object>();

        if("".equals(data)){
            msg="编辑失败";
            map.put("msg",msg);
            return map;
        }
        String [] dataArray=data.split("Θ");
        List<ExamAnswer> listExamAnswer=new ArrayList<ExamAnswer>();
        for(String result:dataArray){
            String[] examAnswerArray=result.split("Γ");
            ExamAnswer examAnswer=new ExamAnswer();
            examAnswer.setContent(examAnswerArray[0]);
            Integer id=Integer.parseInt(examAnswerArray[2]);
            if(id!=-1){
                examAnswer.setId(Integer.parseInt(examAnswerArray[2]));
            }

            if("false".equals(examAnswerArray[1])){
                examAnswer.setIsright("0");
            }else{
                examAnswer.setIsright("1");
            }
            listExamAnswer.add(examAnswer);
        }

        int num=examTestService.editExamTestAndExamAnswer(examTest,listExamAnswer,deleteExamTestIds);
//        examAnswerService.deleteExamByIds(deleteExamTestIds);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }
        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = examTestService.deleteExamByIds(ids);
//            examAnswerService.deleteExamByExamTestIds(ids);
        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
